﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace gui
{
    public partial class frmGui : Form
    {
        #region Private / Validation

        void clear()
        {
            mtxtLB.Clear();
            mtxtUB.Clear();
            mtxtMaxNoDay.Clear();
            mtxtSerial.Clear();
            dtpAddedDate.ResetText();
            //dtpAddedDate.MinDate = DateTime.Now;
            loadFiles();
            if (mtxtSerial.Focused == false)
            {
                mtxtMaxNoDay.Focus();
            }
        }
        bool isValidMaxDay()
        {
            int errors = 0;
            if (mtxtMaxNoDay.Text == string.Empty)
            {
                errors++;
            }
            return errors == 0;
        }
        bool isValidSingle()
        {
            int errors = 0;
            if (mtxtSerial.Text == string.Empty)
            {
                errors++;
            }

            if (isValidMaxDay() == false)
            {
                errors++;
            }
            return errors == 0;
        }
        bool isValidRange()
        {
            int errors = 0;
            if (mtxtLB.Text == string.Empty)
            {
                errors++;
            }
            if (mtxtUB.Text == string.Empty)
            {
                errors++;
            }

            if (isValidMaxDay()==false)
            {
                errors++;
            }

            if (mtxtUB.Text != string.Empty && mtxtUB.Text != string.Empty)
            {
                int lb = int.Parse(mtxtLB.Text);
                int ub = int.Parse(mtxtUB.Text);
                if (lb >= ub)
                {
                    errors++;
                }
            }
            return errors == 0;
        }


        void loadFiles()
        {
            dgvTapes.AllowUserToAddRows = false;
            dgvTapes.Rows.Clear();
            List<string> fileList = Constants.getFileList(Constants.DB_PATH);
            if (fileList != null)
            {                                
                string tape=null;
                foreach (var file in fileList)
                {

                    tape=Constants.readLineFromAFile(
                        string.Format("{0}{1}.{2}",
                        Constants.DB_PATH
                        ,file,Constants.DB_EXT));
                    dgvTapes.Rows.Add(tape,
                        file, Constants.getStatus(true));                    
                }                
            }
        }

        #endregion

        public frmGui()
        {
            InitializeComponent();
        }

        private void frmGui_Load(object sender, EventArgs e)
        {
            dtpAddedDate.Format = DateTimePickerFormat.Custom;
            dtpAddedDate.CustomFormat = Constants.DATE_TIME_FORMAT;
            clear();                        
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void cmdUpdateList_Click(object sender, EventArgs e)
        {
            if (dgvTapes.Rows.Count > 0)
            {
                if (MessageBox.Show(Constants.MSG_Q_WISH_TO_UPDATE, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow dgvr in dgvTapes.Rows)
                    {
                        if (dgvr.Cells[Constants.INDEX_TAPE_STATUS].Value.ToString() == Constants.STATUS_NEW)
                        {
                            try
                            {
                                Constants.createFile(string.Format("{0}{1}", Constants.DB_PATH,
                                    Constants.buildFileName(dgvr.Cells[Constants.INDEX_TAPE_DATES].Value.ToString())), dgvr.Cells[Constants.INDEX_TAPE_SERIAL].Value.ToString());
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }                    
                }
            }
            clear();
        }

        private void cmdTmpAddRange_Click(object sender, EventArgs e)
        {
            if (isValidRange())
            {
                int lb = int.Parse(mtxtLB.Text);
                int ub = int.Parse(mtxtUB.Text);
                int interval = int.Parse(mtxtMaxNoDay.Text);

                DateTime tmp = dtpAddedDate.Value;
                string status = null;

                for (int i = lb; i <= ub; i++)
                {
                    status = Constants.getStatus(Constants.fileExists(string.Format("{0}{1}", Constants.DB_PATH,
                        tmp.ToString(Constants.DATE_TIME_FORMAT))));
                    tmp = tmp.AddDays(interval);
                    dgvTapes.Rows.Add(i.ToString().PadLeft(7, Constants.PADD_CHAR),
                        tmp.ToString(Constants.DATE_TIME_FORMAT), status);
                }
            }
            else
            {
                MessageBox.Show(Constants.MSG_ERROR_INVALID_FORMAT, this.Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
                clear();
            }
        }

        private void cmdTmpAddSingle_Click(object sender, EventArgs e)
        {
            if (isValidSingle())
            {
                DateTime tmp = dtpAddedDate.Value.AddDays(int.Parse(mtxtMaxNoDay.Text));
                string status = Constants.getStatus(Constants.fileExists(string.Format("{0}{1}", Constants.DB_PATH,
                        tmp.ToString(Constants.DATE_TIME_FORMAT))));
                dgvTapes.Rows.Add(mtxtSerial.Text.PadLeft(Constants.SERIAL_NUMBER_LENGTH,
                    Constants.PADD_CHAR), tmp.ToString(Constants.DATE_TIME_FORMAT), status);
            }
            else
            {
                MessageBox.Show(Constants.MSG_ERROR_INVALID_FORMAT, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                clear();
            }
        }


    }
}
